package light.head.dao.site;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.FileInfo;
import light.head.model.WorkGuideAnnex;
import light.head.model.WorkGuideDetail;

@Service
public class WorkGuideDetailDAO extends BaseDAO<WorkGuideDetail, Serializable> {

	private static final String sql_findAnnex = "FROM WorkGuideAnnex wga WHERE wga.wgdc = ?";
	private static final String sql_queryDetailList = "FROM WorkGuideDetail wgd WHERE wgd.wgpc = ? AND wgd.deleted = 0";
	private static final String sql_queryDetail = "FROM WorkGuideDetail wgd WHERE wgd.wgpc = ? AND wgd.deleted = 0";
	public RESULT findByMap(Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM WorkGuideDetail wgd WHERE 1 = 1");
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();
			String aibn = map.get(Parm.AIBN);
			String aisn = map.get(Parm.AISN);
			String ao = map.get(Parm.AO);
			String deleted = map.get(Parm.DELETED);
			if (aibn != "" && isNotNull(aibn)) {
				hql.append(" AND wgd.aibn LIKE ? ");
				list.add("%" + aibn + "%");
			}
			if (aisn != "" && isNotNull(aisn)) {
				hql.append(" AND wgd.aisn LIKE ? ");
				list.add("%" + aisn + "%");
			}
			if (ao != "" && isNotNull(ao)) {
				hql.append(" AND wgd.ao LIKE ? ");
				list.add("%" + ao + "%");
			}
			if (deleted != "" && isNotNull(deleted)) {
				hql.append(" AND wgd.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		hql.append(" ORDER BY wgd.code ");
		return findToPageBean(hql.toString(), pageBean, list);
	}

	/**
	 * 根据拼接好的文件编号序列获取对应的文件列表
	 */
	public List<FileInfo> findFileByCodes(String codes) {
		if (!isNotNull(codes)) {
			return null;
		}
		String sql = "FROM FileInfo f WHERE f.code IN (" + codes + ")";
		List<FileInfo> list = find(sql);
		return list;
	}

	public List<WorkGuideAnnex> findAnnexByCode(Integer code) {
		return super.find(sql_findAnnex, code);
	}

	public RESULT deleteAnnexNotInCodes(Integer code, List<Object> codes) {
		StringBuffer sql = new StringBuffer();
		List<Object> parm = new ArrayList<Object>();
		parm.add(code);
		for (Object o : codes) {
			if(o != null){
				sql.append("?,");
				parm.add(o);
			}
		}
		if (sql.length() > 0) {
			sql.insert(0, " AND wga.code NOT IN(").deleteCharAt(sql.length() - 1).append(")");
		}
		sql.insert(0, "DELETE FROM WorkGuideAnnex wga WHERE wga.wgdc = ?");
		execute(sql.toString(), parm.toArray());
		return RESULT.SUCCESS;
	}
	public List<WorkGuideDetail> getList(Integer code) {
		return super.find(sql_queryDetailList, code);
	}
	public WorkGuideDetail getDetail(Integer code) {
		return (WorkGuideDetail)super.get(sql_queryDetail, code);
	}
}
